package com.uyaer.engine.display
{
	import com.uyaer.engine.Engine;
	import com.uyaer.engine.event.EventManager;
	import com.uyaer.engine.event.EventType;
	import com.uyaer.engine.event.MyEvent;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	public class Camera extends Sprite
	{
		public static const WIDTH:int = 1000;
		public static const HEIGHT:int = 550;
		public var engine:Engine;
		public var scene:Scene;
		public function Camera()
		{
			EventManager.addEvent(EventType.MINE_POS_CHANGE, _onPosChangeEvent);
		}
		
		private function _onPosChangeEvent(e:MyEvent):void{
			lookAt(Point(e.data));
		}
		
		public function lookAt(p:Point):void{
			var offX:int = Math.min(scene.background.width - WIDTH , Math.max(0 , p.x - width * 0.5));
			scene.bgLayer.x = -offX;
		}
		
		public function resize():void{
			var sw:int = scene.stage.stageWidth;
			var sh:int = scene.stage.stageHeight;
			var offX:int = Math.max(0 , (sw - WIDTH) * 0.5);
			var offY:int = Math.max(0 , (sh - HEIGHT) * 0.5);
			graphics.clear();
			graphics.beginFill(0);
			graphics.drawRect(offX, offY, WIDTH, HEIGHT);
			graphics.endFill();
			
			scene.resetPostion(getSize().x , getSize().y);
		}
		
		public function getSize():Rectangle{
			return this.getBounds(scene.stage);
		}
		
		public function bind(engine:Engine):void{
			this.engine = engine;
			this.scene = engine.scene;
			scene.stage.addChildAt(this,1);
			resize();
			scene.bgLayer.mask = this;
		}
	}
}